package com.zmn.oms.task.job.grab;

import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.spring.tasktracker.JobRunnerItem;
import com.github.ltsopensource.spring.tasktracker.LTS;
import com.github.ltsopensource.tasktracker.Result;
import com.zmn.common.dto.ResultDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.consts.GlobalConsts;
import com.zmn.manager.lts.interfaces.task.TaskManager;
import com.zmn.oms.common.utils.DateTimeUtil;
import com.zmn.oms.manager.utils.TaskConsts;
import com.zmn.oms.model.query.grab.NonGridGrabOrderQuery;
import com.zmn.oms.services.interfaces.grab.NonGridGrabOrderService;
import com.zmn.oms.services.interfaces.graborder.GrabOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;

/**
 * 类描述：定时删除抢单池信息
 *
 * @author wangxiaokun
 * @since 2021/07/27 15:59
 */
@Slf4j
@LTS
public class GrabOrderJob {

    @Resource
    TaskManager taskManager;
    @Autowired
    GrabOrderService grabOrderService;
    @Autowired
    NonGridGrabOrderService nonGridGrabOrderService;

    /**
     * 每天早上1点执行
     */
    @PostConstruct
    void init() {
        Job job = new Job();
        job.setTaskId(TaskConsts.TASK_ID_DELETE_GRAB_ORDER_JOB);
        job.setParam(TaskManager.SHARD_FIELD, TaskConsts.TASK_ID_DELETE_GRAB_ORDER_JOB);
//        job.setCronExpression("0 0/1 * * * ? ");
        job.setCronExpression("0 0 1 1/1 * ? ");
        job.setReplaceOnExist(true);
        ResultDTO result = taskManager.submitTask(job);
        if (!result.getStatusBool()) {
            log.error("#oms#healthcardpunching# job submit fail...");
        }
    }

    /**
     * 每隔7天早上1点执行
     */
    @PostConstruct
    void initNonGridGrab() {
        Job job = new Job();
        job.setTaskId(TaskConsts.TASK_ID_DELETE_NON_GRID_GRAB_ORDER_JOB);
        job.setParam(TaskManager.SHARD_FIELD, TaskConsts.TASK_ID_DELETE_NON_GRID_GRAB_ORDER_JOB);
//        job.setCronExpression("0 0/1 * * * ? ");
        job.setCronExpression("0 0 1 1/7 * ? ");
        job.setReplaceOnExist(true);
        ResultDTO result = taskManager.submitTask(job);
        if (!result.getStatusBool()) {
            log.error("#oms#healthcardpunching# job submit fail...");
        }
    }

    /**
     * 每天早上1点执行APP推送健康打卡
     * @param job
     * @return
     */
    @JobRunnerItem(shardValue = TaskConsts.TASK_ID_DELETE_GRAB_ORDER_JOB)
    public Result deleteGrabOrderJob(Job job) {
        log.info("#oms#delete#grab#order# 开始执行删除抢单池任务。");
        Date date = DateTimeUtil.getAppointDay(-2);
        grabOrderService.deleteGrabOrderByDate(date);

        log.info("#oms#delete#grab#order# 开始执行删除抢单池任务。删除{}之前的抢单信息", DateUtil.dateFormatToString(date, DateUtil.FORMAT_DEFAULT));
        return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
    }

    /**
     * 每天早上1点执行APP推送健康打卡
     * @param job
     * @return
     */
    @JobRunnerItem(shardValue = TaskConsts.TASK_ID_DELETE_NON_GRID_GRAB_ORDER_JOB)
    public Result deleteNonGridGrabOrderJob(Job job) {
        log.info("#oms#delete#nongridgrab#order# 开始执行删除抢单池任务。");
        Date date = DateTimeUtil.getAppointDay(-7);
        NonGridGrabOrderQuery nonGridGrabOrderQuery = new NonGridGrabOrderQuery();
        nonGridGrabOrderQuery.setValidStatus(GlobalConsts.NO);
        nonGridGrabOrderQuery.setEndUpdateTime(date);
        nonGridGrabOrderService.deleteByQuery(nonGridGrabOrderQuery);

        log.info("#oms#delete#grab#order# 开始执行删除抢单池任务。删除{}之前的抢单信息", DateUtil.dateFormatToString(date, DateUtil.FORMAT_DEFAULT));
        return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
    }
}
